<?php

/*
 * 分析功能核心入口：读取数据源;
 */

namespace Common\Controller;

/**
 * Description of FetchDataController
 *
 * @author chenghui
 */
class FetchDataController {
    protected $code;//分析代号
    protected $type;//0-交叉分析；1-排名分析
    protected $isCache;//1-表示需要缓存，0表示不需要
    protected $prefix;//统计字段前缀表达式
    protected $DataType;//分析的基础配置类型
    protected $DBType;//数据库类型

    protected $allAxis=array();//所有维度值：序列化后
    protected $xAxis=array();//x维度-数组：序列化后
    protected $yAxis=array();//y维度-数组：序列化后
    protected $vAxis=array();//v维度-数组：序列化后
    protected $ranking=array();//排名参数-数组：序列化后
    protected $count=array();//统计参数-数组：序列化后
    protected $condition=array();//原始条件-数组：序列化后
    protected $where=array();//原始条件参数数组：去空值后
    protected $echarts=array();//图表参数数组：去空值后
    protected $DataBase='';//数据源
    
    protected $regionColumn=array();//区间段数组
    protected $xNums=0;//x维度数量
    protected $yNums=0;//y维度数量
    protected $vNums=0;//v维度数量
    protected $levelNums=1;//表头层级数

    protected $DataBaseConfig=array();//维度的基础配置-数组
    protected $AxisConfig=array();//维度配置信息-数组
    protected $UomConfig=array();//计量单位配置信息-数组
    protected $CountConfig=array();//统计方式配置信息-数组
    protected $BdataSpecial=array();//维度的特殊配置-数组
    protected $ChartFormat=array();//图表配置-数组
    
    protected $Header=array();//表头数组
    protected $HeaderConfig=array();//表头数组中代号与字段对照关系
    protected $Footer=array();//底部合计数组
    protected $Left=array();//左侧标题数组
    protected $Right=array();//右侧合计数组
    protected $Total=array();//右侧最终合计数组
    protected $tempTableArr=array();//临时表
    protected $error;//错误
    
    public function __construct($param){
        //0.初始化分析代号
        $this->code = empty($param['code'])?'Bdata':$param['code'];//分析代号
        $this->type = empty($param['type'])?0:$param['type'];
        $this->isCache = 1;//需要使用缓存
        $this->prefix = 'C_';
        
        //1.初始化配置信息
        $this->initBaseConfig();
        
        //2.初始化X维度
        if(empty($param['xAxis'])){
            $this->xNums = 0;
        }else{
            $this->xAxis=$this->parseAxis($param['xAxis']);
            $this->xNums = count($this->xAxis);
        }

        //3.初始化Y维度
        if(empty($param['yAxis'])){
            $this->yNums=0;
        }else{
            $this->yAxis=$this->parseAxis($param['yAxis']);
            $this->yNums=count($this->yAxis);
        }

        //4.初始化V维度
        if(empty($param['vAxis'])){
            $this->vNums=0;
        }else{
            $this->vAxis=$this->parseCount($param['vAxis']);
            $this->vNums=count($this->vAxis);
        }
        
        //5.初始化ranking参数
        if(empty($param['ranking'])){
            $this->ranking=array();
        }else{
            $this->ranking=$param['ranking'];
        }
        
        //6.初始化count参数:横纵向统计
        if(empty($param['count'])){
            $this->count=array();
        }else{
            $this->count=$param['count'];
        }
        
        //7.除空值后条件数组
        //初始化条件值
        $this->condition = array();
        $this->condition = $this->setWipeNull($param['where']);

        //8.初始化条件表达式(不带where关键字)
        
        $this->where = $this->initWhereCondition();
      
        //9.设置统计数据的小数位
        $this->decimal=6;
        
        //10.初始化图表类型：优先取自定义参数，再取配置表，最后取默认值
        if(empty($param['echarts'])){
            $this->echarts = $this->ChartFormat;
        }else{
            if(is_array($param['echarts'])){
                foreach ($param['echarts'] as $key){
                    $this->echarts[$key]=$this->ChartFormat[$key];
                }
            }
        }
        
        //11.初始化表头层级数
        if($this->vNums==1){
            $this->levelNums = ($this->xNums>0)?$this->xNums:1;
        }else{
            $this->levelNums = ($this->yNums>0)?$this->xNums+1:$this->xNums;
        }
        
        unset($param);
    }
    
    /**
     * @初始化基础配置信息
     */
    private function initBaseConfig(){ 
        $Base = new ConfigureController($this->code,$this->isCache);
        
        //获取基础配置
        $Config = $Base->initBaseConfig();
        //dump($Config['Uom']);exit();
        $this->DataBaseConfig = $Config['Base'];
        $this->AxisConfig = $Config['Axis'];
        $this->UomConfig = $Config['Uom'];
        $this->CountConfig = $Config['Count'];
        
        //获得特殊数据的配置
        $this->BdataSpecial = $Base->initBdataSpecial();
        
        //获得图表类型
        $this->ChartFormat = $Base->initChartFormat();
        
        //获取数据库类型
        $this->DBType = $Base->getDBType();
    }
    
    
    /**
     * 解析参数中的维度值
     * @access array()
     * @return array()
     */
    private function parseAxis($axis){
        $result = array();

        foreach ($axis as $key=>$val){
            $key = strtoupper($key);
            $result[]=$key;
            $this->allAxis[]=$key;//记录所有维度值
            
            if($this->AxisConfig[$key]['FieldType']==3){
                //追加处理区间段表达式
                if(!empty($val[1])){//存在单位编码时
                    $rate = (isset($this->UomConfig[$val[1]]['Rate']))?$this->UomConfig[$val[1]]['Rate']:1;

                    $this->regionColumn[$key]=$this->setRegionDetail($val[0],$rate,$this->UomConfig[$val[1]]['UomName']);
                }else{//无传入编码时，取默认值
                    $this->regionColumn[$key]=$this->setRegionDetail($val,1,$this->AxisConfig[$key]['UomName']);
                }  
            }
        }
        return $result;
    }
    
    /**
     * 解析参数中的统计值
     * @access array()：V维度数组
     * @return array()
     */
    private function parseCount($axis){
        $result = array();
        
        foreach ($axis as $k=>$val){
            $key = strtoupper($k);
            
            if ($this->AxisConfig[$key]['FieldType'] == 3){
                if(!empty($val[0])&&!empty($val[1])){
                    $cou=  explode(',', $val[0]);
                    foreach ($cou as $count){
                        $c = strtoupper($count);
                        $result[]=array('k'=>$key,'c'=>$c,'u'=>$val[1]);
                    }
                }
            }
        }
        return $result;
    }
    
    
    /**
     * @方法：根据键值及相对应的区间段
     * @param type $regArr
     * @param type $rate 换算率
     * @param type $uom 单位名称
     * @return string
     */
    protected function setRegionDetail($regArr,$rate,$uom){
        $i=1;//用于判断首次
        $resArr=array();//记录各区间段的ID及TITLE

        if(is_string($regArr)){
            $regArr = explode('|', $regArr);
        }
        
        foreach($regArr as $valArr){
            $aArr=  explode('-', $valArr);
            
            $min=$aArr[0]*$rate;
            $max=$aArr[1]*$rate;
                     
            $minName = $aArr[0].$uom;
            $maxName = $aArr[1].$uom;
                        
            $rName=$aArr[0].'-'.$aArr[1].$uom;
            
            if($i==1){
                if($min>0){//最小值不为0
                    $resArr[$i]['n']=$i;
                    $resArr[$i]['i']=0;
                    $resArr[$i]['a']=$min;
                    $resArr[$i]['t']='<='.$minName;

                    $i++;
                    $resArr[$i]['n']=$i;
                    $resArr[$i]['i']=$min;
                    $resArr[$i]['a']=$max;
                    $resArr[$i]['t']=$rName;
                }else{//最小值为0
                    $resArr[$i]['n']=$i;
                    $resArr[$i]['i']=0;
                    $resArr[$i]['a']=$max;
                    $resArr[$i]['t']='<='.$maxName;
                }
            }else{
               $resArr[$i]['n']=$i;
               $resArr[$i]['i']=$min;
               $resArr[$i]['a']=$max;
               $resArr[$i]['t']=$rName;
            }
            $i++;
        }
        //最后追加最大区间段
        $resArr[$i]['n']=$i;
        $resArr[$i]['i']=$max;
        $resArr[$i]['a']=0;
        $resArr[$i]['t']='>'.$maxName;
        
        return $resArr;
    }
    
    
    /**
     * @清除掉条件数组中的空值条件
     * @param type $array
     * @return type
     */
    protected function setWipeNull($array){
        $where=array();
        
        foreach($array as $k=>$val){
            $key = strtoupper($k);
            if(is_array($val)){
                $v = array_filter($val);
            }else{
                $v = $val;
            }
            
            if(!empty($v)){
                $where[$key] = $val;
            }
        }
        return $where;
    }
    
    
    /**
     * 方法：处理条件子句
     * 参数：传入条件数组$where
     * 分支：支持不带关系符的条件，以及带关系符的条件
     * 返回：字符串
     */
    private function initWhereCondition(){
  
        //1.处理系统配置强制需要的查询条件：主要用于是否去空值
        $nArr = $this->parseIsNullCondition($this->allAxis);
    
        //2.处理参数传入的条件子句：参数处理
        $wArr = $this->parseWhereCondition($this->condition);

        $res = array_merge($nArr,$wArr);
        
        return implode(' and ', $res);
    }

    /**
     * @解析配置表中不允许为空的字段表达式
     * @param type $allAxis
     * @return string
     */
    private function parseIsNullCondition($allAxis){
        $arr = array();
    
        foreach ($allAxis as $key){
            if($this->AxisConfig[$key]['IsNull']>0){
                switch ($this->AxisConfig[$key]['FieldType']){
                    case 1://对照
                        $arr[] = $this->AxisConfig[$key]['DataColumn'].' > 0';
                        break;
                    case 2://日期
                        $arr[] = $this->AxisConfig[$key]['DataColumn'].' is not null';
                        break;
                    case 3://统计
                        if(empty($this->AxisConfig[$key]['Expression'])){
                            if(strpos($this->AxisConfig[$key]['DataColumn'],'/')){
                                $val = explode('/', $this->AxisConfig[$key]['DataColumn']);
                                $arr[] = $val[1].' > 0'; 
                            }else{
                                $arr[] = $this->AxisConfig[$key]['DataColumn'].' > 0';
                            }                             
                        }else{
                            $arr[] = $this->AxisConfig[$key]['Expression'].' > 0';
                        }                        
                        break;
                    case 4://文本
                        $arr[] = $this->AxisConfig[$key]['DataColumn'].' is not null';
                        break;
                }                
            }
            
        }
        return $arr;
    }

    /**
     * @方法：处理日期型字段值
     * @param type $Expression
     * @param type $DataColumn
     * @return type
     */
    private function setDateConditionExp($Expression,$DataColumn){
        $arr = strtoupper($Expression);
        switch ($arr){
            case 'Y'://年份
                //$res = 'YEAR('.$DataColumn.')';//MSSQL
                $res = 'YEAR('.$DataColumn.')';//mysql
                break;
            case 'YM'://月份
                $res = 'MONTH('.$DataColumn.')';
                break;
            case 'YQ'://季度
                //$res = 'DATEPART(QQ,'.$DataColumn.')';
                $res = 'QUARTER('.$DataColumn.')';
                break;
            case 'YW'://周数
                //$res = 'DATEPART(WW,'.$DataColumn.')';
                $res = 'week('.$DataColumn.',1)';
                break;
            case 'HH'://小时
                $res = 'HOUR('.$DataColumn.')';
                break;
            case 'D':
            default :
                $res = $DataColumn;
                break;
        }
        return $res;
    }
    
    
    /**
     * @用于处理多表时具有计算功能的字段表达式
     * @param type $Expression
     * @param type $rulesArr
     * @return string
     */
    private function setNumConditionExp($Expression,$DataColumn){
        $res='';
        if(!empty($Expression)){
            $res = $Expression;            
        }else{
            if(strpos($DataColumn,'/')){
       
                $val = explode('/', $DataColumn);
                $de = '/';
              
            }elseif(strpos($DataColumn,'*')){
                $val = explode('*', $DataColumn);
                $de = '*';
            }elseif(strpos($DataColumn,'+')){
                $val = explode('+', $DataColumn);
                $de = '+';
            }elseif(strpos($DataColumn,'-')){
                $val = explode('-', $DataColumn);
                $de = '-';
            }
            
            //若不存在字段表达式
            if(!empty($de)){
                $key1 = strtoupper($val[0]);
                $key2 = strtoupper($val[1]);
             
                $res = $this->AxisConfig[$key1]['DataColumn'].$de.$this->AxisConfig[$key2]['DataColumn'];
            }else{
                $res = $DataColumn;
            }            
        }
        return $res;
    }
    
    /**
     * @解析作为条件时，字段表达式
     * @param type $type
     * @param type $Expression
     * @param type $DataColumn
     * @return type
     */
    private function getConditionColumn($type,$Expression,$DataColumn){
        $Exp = strtoupper($Expression);
 
        switch ($type){
            case 1://对照
            case 4://文本
            default :
                $column = $DataColumn;
                break;
            case 2://日期
                $column = $this->setDateConditionExp($Expression,$DataColumn);
                break;
            case 3://统计
                $column = $this->setNumConditionExp($Expression,$DataColumn);
                break;
        }
           
        return $column;
    }

    /**
     * @解析条件项成最终的条件表达式
     * @param type $where
     * @return type
     */
    private function parseWhereCondition($where){
        $arr = array();
     
        foreach($where['DUTYSS'] as $k=>$v){
                $where['DUTYSS'][$k] =  "'".$v."'";
        }
        
        //读取对照表信息
        foreach($where as $key=>$val){
            $column = $this->getConditionColumn($this->AxisConfig[$key]['FieldType'],$this->AxisConfig[$key]['Expression'],$this->AxisConfig[$key]['DataColumn']);

            if(is_array($val)){//条件值为数组时
                if(isset($val['relation'])&&isset($val['values'])){//复杂条件
                  
                    $arr[] = $this->getComplexCodition($key,$column,$val['relation'],$val['values']);
                   
                }elseif(isset($val['r'])&&isset($val['v'])){//复杂条件
             
                    $arr[] = $this->getComplexCodition($key,$column,$val['r'],$val['v']);
                }else{//综合查询
                    $arr[]=$this->getMutiCodition($key,$column,$val);
                
                }
            }else{//条件为字符串时
                $arr[]=$this->getComplexCodition($key,$column,'EQ',$val);   
            }
          
        }
        
        return $arr;
    }
    
    /**
     * @综合查询时条件表达式
     * @param type $key
     * @param type $column
     * @param type $val
     * @return type
     */
    private function getMutiCodition($key,$column,$val){

        switch ($this->AxisConfig[$key]['FieldType']){
            case 1://对照
                $result = $this->getComplexCodition($key,$column,'IN',$val);
                break;
            case 2://日期
                $result = $this->getComplexCodition($key,$column,'BT',$val);
                break;
            case 3://统计
                $result = $this->getComplexCodition($key,$column,'BT',$val);
                break;
            case 4://文本
                $result = $this->getComplexCodition($key,$column,'EQ',$val);
                break;
        }
        return $result;
    }
     /**
     * @根据参数处理成where子句：当为复杂条件表达式时，也作为一切条件表达式的基础方法
     * @param type $key 键
     * @param type $relation 关系
     * @param type $val 值
     * @param type $isMultiSheet 是否多表
     * @param type $rulesArr 配置规则
     * @return type 字符串表达式
     */
    private function getComplexCodition($key,$column,$relation='EQ',$val){

        $rel = strtoupper($relation);
        $FieldType = $this->AxisConfig[$key]['FieldType'];
     
        switch ($rel){
            case 'EQ':
            case 'EQUAL'://等于
                $value = is_array($val)?implode(',', $val):$val;
                switch ($FieldType){
                    case 1:
                        $s = $column.' = '.$value;
                    case 3:
                        if(strpos($column,'/')){
                            $e = explode('/', $column);
                            $s = $e[0].' = '.$e[1].'*'.$value;
                        }else{
                            $s = $column.' = '.$value;
                        }                        
                        break;
                    case 2:
                    case 4:
                        $s = $column." = '".$value."'";
                        break;
                }
                break;
            case 'NEQ':
            case 'NOTEQUAL'://不等于            
                $value = is_array($val)?implode(',', $val):$val;
                switch ($FieldType){
                    case 1:
                        $s = $column.' <> '.$value;
                    case 3:
                        if(strpos($column,'/')){
                            $e = explode('/', $column);
                            $s = $e[0].' <> '.$e[1].'*'.$value;
                        }else{
                            $s = $column.' <> '.$value;
                        }                        
                        break;
                    case 2:
                    case 4:
                        $s = $column." <> '".$value."'";
                        break;
                }
                break;
            case 'LIKE'://模糊查询
                $value = is_array($val)?implode(',', $val):$val;
                
                switch ($FieldType){//2及3类无模糊查询的需求
                    case 1:
                        $s = $column.' in ('.'select '.$this->AxisConfig['ContrastId'].' from '.$this->AxisConfig['ContrastSheet'].' where '.$this->AxisConfig['ContrastName']." like '%".$value."%')";
                        break;
                    case 4:
                        $s = $column." like '%".$value."%'";
                        break;
                }
                break;
            case 'NOTLIKE'://模糊查询
                $value = is_array($val)?implode(',', $val):$val;
                
                switch ($FieldType){//2及3类无模糊查询的需求
                    case 1:
                        $s = $column.' in ('.'select '.$this->AxisConfig['ContrastId'].' from '.$this->AxisConfig['ContrastSheet'].' where '.$this->AxisConfig['ContrastName']." not like '%".$value."%')";
                        break;
                    case 4:
                        $s = $column." not like '%".$value."%'";
                        break;
                }
                break;
                    case 'IN':
            case 'INCLUDED'://包含            
                switch ($FieldType){
                    case 1://对照
                        $value = is_array($val)?implode(',', $val):$val;
                        $s = $column.' in ('.$value.')';
                        break;
                    case 2://日期
                    case 4://文本
                        $s = $column.' in ('.$this->getAddQuotation($val).')';
                        break;
                }
                break;
            case 'NOTIN':
            case 'NOTINCLUDED'://不包含              
                switch ($FieldType){
                    case 1://对照
                        $value = is_array($val)?implode(',', $val):$val;
                        $s = $column.' not in ('.$value.')';
                        break;
                    case 2://日期
                    case 4://文本
                        $s = $column.' not in ('.$this->getAddQuotation($val).')';
                        break;
                }
                break;
            case 'BT':
            case 'BETWEEN'://两者之间     
                
                if(is_array($val)){
                    $s = $this->setBetweenExp($column,$val,$FieldType,$key);                    
                }elseif(is_string($val)){
                    $arr=  explode(',', $val);
                    $s = $this->setBetweenExp($column,$arr,$FieldType,$key);
                }
                break;
            case 'UL':
            case 'ULIMIT'://上限，即：大于上限，小于等于下限                
                if(is_array($val)){
                    $s = $this->setUpLimitExp($column,$val,$FieldType);                    
                }elseif(is_string($val)){
                    $arr=  explode(',', $val);
                    $s = $this->setUpLimitExp($column,$arr,$FieldType);
                }
                break;
        }
        
        return $s;
    }
    
    
    /**
     * @将值追加'号
     * @param type $str 字符串或数组
     * @return type 字符串
     */
    private function getAddQuotation($str){
        $col = array();
        if(is_string($str)){
            $array=  explode(',', $str);
            foreach($array as $val){
                $col[]="'".$val."'";
            }
        }elseif(is_array($str)){
            foreach($str as $val){
                $col[] = "'".$val."'";
            }
        }
        $string=  implode($col, ',');
        
        return $string;
    }
    
    /**
     * @处理区间段值时的表达式
     * @param type $column
     * @param type $val 数组，区间段
     * @return string
     */
    private function setBetweenExp($column,$val,$FieldType,$key){
        $s='';
 
        switch ($FieldType){
            case 2://日期
                $b = $val[0];
                $e = $val[1];
                if(!empty($b) && !empty($e)){
                    $s = $column." between '".$b."' and '".$e."'"; 
                }elseif(empty($b) && !empty($e)){
                    $s = $column." <= '".$e."'"; 
                }elseif(!empty($b) && empty($e)){
                    $s = $column." >= '".$b."'"; 
                }
                break;
            case 3:
                $b = empty($val[0])?0:$val[0];
                $e = empty($val[1])?0:$val[1];
                //公告地价  成交地价  溢价率
         
                if($this->code == 'Eland' || $this->code == 'Eland_gy'){
                    if($b>0 && $e>0){
                            $s = $column.' between '.$b.' and '.$e; 
                        }elseif($b==0 && $e>0){
                            $s = $column.' <= '.$e; 
                        }elseif($b>0 && $e==0){
                            $s = $column.' >= '.$b; 
                        }
                }else{
                    if(strpos($column,'/')){
                        $c = explode('/', $column);
                        if($b>0 && $e>0){
                            $s = $c[0].' between '.$c[1].'*'.$b.' and '.$c[1].'*'.$e;
                        }elseif($b==0 && $e>0){
                            $s = $c[0].' <= '.$c[1].'*'.$e; 
                        }elseif($b>0 && $e==0){
                            $s = $c[0].' >= '.$c[1].'*'.$b; 
                        }            
                    }else{
                        if($b>0 && $e>0){
                            $s = $column.' between '.$b.' and '.$e; 
                        }elseif($b==0 && $e>0){
                            $s = $column.' <= '.$e; 
                        }elseif($b>0 && $e==0){
                            $s = $column.' >= '.$b; 
                        }
                    }
                }
                break;
        }
        return $s;
    }
    
    /**
     * @解析在上限范围内的条件关系
     * @param type $column
     * @param type $val
     * @param type $FieldType
     * @return string
     */
    private function setUpLimitExp($column,$val,$FieldType){
        $s='';
        switch ($FieldType){
            case 2://日期
                $b = $val[0];
                $e = $val[1];
                if(!empty($b) && !empty($e)){
                    $s = $column." > '".$b."' and ".$column." <= '".$e."'"; 
                }elseif(empty($b) && !empty($e)){
                    $s = $column." <= '".$e."'"; 
                }elseif(!empty($b) && empty($e)){
                    $s = $column." > '".$b."'"; 
                }
                break;
            case 3://数字
                $b = empty($val[0])?0:$val[0];
                $e = empty($val[1])?0:$val[1];

                if(strpos($column,'/')){
                    $c = explode('/', $column);

                    if($b>0 && $e>0){
                        $s = $c[0].' > '.$c[1].'*'.$b.' and '.$c[0].' <= '.$c[1].'*'.$e;
                    }elseif($b==0 && $e>0){
                        $s = $c[0].' <= '.$c[1].'*'.$e; 
                    }elseif($b>0 && $e==0){
                        $s = $c[0].' > '.$c[1].'*'.$b; 
                    }            
                }else{
                    if($b>0 && $e>0){
                        $s = $column.' between '.$b.' and '.$e; 
                        $s = $column.' > '.$b.' and '.$column.' <= '.$e; 
                    }elseif($b==0 && $e>0){
                        $s = $column.' <= '.$e; 
                    }elseif($b>0 && $e==0){
                        $s = $column.' > '.$b; 
                    }
                }
                break;
        }
        return $s;
    }
    
    
    /**
     * @解析最初始的数据源：可以为表名、临时表名、SQL语句
     * @支持存储过程的解析
     * @return string
     */
    private function getBaseDataOrigin(){
        //初始化表来源数：以确定SQL是否加表名
        $this->DataType = strtoupper($this->DataBaseConfig['DataType']);

        switch ($this->DataType){
            case 'TABLE'://表名
            default :
                $this->DataBase = $this->DataBaseConfig['DataExp'];
                break;
            case 'SQL'://SQL语句
                $this->DataBase = '('.$this->DataBaseConfig['DataExp'].') as OriginTB';
                break;
            case 'STORED'://存储过程
                $DataBase = 'Stored_'.date("Ymdhis");
                $this->tempTableArr[] = $DataBase;

                $param = $this->DataBaseConfig['Param'];
                $allAxis = implode(',', $this->allAxis);

                $bind=$this->setStoredBind($DataBase,$allAxis,$param);
                M()->prepare($this->DataBaseConfig['DataExp'],$bind);
                $this->DataBase = $DataBase;
                break;
        }       
    }
    
    
    /**
     * @说明：将条件参数转换成bind参数数组。仅用于存储过程的调用
     * @param type $DataBaseParam
     * @return string
     */
    private function setStoredBind($DataBase,$allAxis,$Param){  
        $bind=array();
        $array = explode(';', $Param);//存储过程参数，分号分隔
        foreach ($array as $v){
            $line = explode('=', $v);
            $key = $line[0];
            $val = $line[1];

            if($val=='T'){//常量：表示数据表名
                $bind[$key]=$DataBase;
            }elseif($val=='allAxis'){//表示维度
                $bind[$key]=$allAxis;
            }else{
                if(strpos($val,':')){//存在区间段值时，设定0为最小值，1为最大值
                    $arr = explode(':', $val);
                    $k = strtoupper($arr[0]);
                    $values = $this->condition[$k][$arr[1]];
                    $bind[$key]=  !empty($values)?$values:'';
                }else{
                    $k = strtoupper($val);
                    if(isset($this->condition[$k])){
                        if(is_array($this->condition[$k])){
                            $bind[$key] = implode(',', $this->condition[$k]);
                        }else{
                            $bind[$key] = $this->condition[$k];
                        }
                    }else{
                        $bind[$key]=  '';
                    }
                }
            }
        }
        return $bind;
    }
    
    
    /**
     * @方法：处理一些日期常量的表达式,主要用于解析时间的中文表达式
     * @param type $Expression 表达式 Expression
     * @param type $DataColumn 字段值 DataColumn
     * @return string
     */
    private function setDateExp($Expression,$DataColumn){
        switch ($Expression){
            case 'Y'://年份
                //$res = "CONVERT(nvarchar(4),YEAR(".$DataColumn."))+'年'";//MSSQL
                $res = "CONCAT(year(".$DataColumn."),'年')";
                break;
            case 'YM'://年月
                //$res = "CONVERT(nvarchar(4),YEAR(".$DataColumn."))+'年'+RIGHT('00'+CAST(MONTH(".$DataColumn.") AS VARCHAR(2)),2)+'月'";
                $res = "CONCAT(year(".$DataColumn."),'年',month(".$DataColumn."),'月')";
                break;
            case 'YW'://年周
                //$res = "CONVERT(varchar(10),DATEADD(wk, DATEDIFF(wk,0,DateAdd(dd,-1,".$DataColumn.")),0),120)";//周一作为周的第一天,主要注意周日要减一天
                //$res .= "+'至'+";
                //$res .= "CONVERT(varchar(10),DATEADD(wk, DATEDIFF(wk,0,DateAdd(dd,-1,".$DataColumn.")),6),120)";//周日作为周的最后一天
                
                $res1 = "date_sub(".$DataColumn.",INTERVAL WEEKDAY(".$DataColumn.") + 1 DAY)";
                $res2 = "date_sub(".$DataColumn.",INTERVAL WEEKDAY(".$DataColumn.") - 5 DAY)";
                
                $res = "CONCAT(".$res1.",'至',".$res2.")";
                break;
            case 'YQ'://年季
                //$res = "CONVERT(nvarchar(4),YEAR(".$DataColumn."))+'年第'+CONVERT(nvarchar(2),DATEPART(QQ,".$DataColumn."))+'季度'";
                $res = "CONCAT(year(".$DataColumn."),'年第',QUARTER(".$DataColumn."),'季度')";
                break;
            case 'YB'://半年
                //$res = "CONVERT(nvarchar(4),YEAR(".$DataColumn."))+'年'+(case when MONTH(".$DataColumn.")<=6 then '上半年' else '下半年' end)";
                $res = "CONCAT(year(".$DataColumn."),'年',(case when MONTH(".$DataColumn.")<=6 then '上半年' else '下半年' end))";
                break;
            case 'HH'://小时24小时制
                //$res = "CONVERT(nvarchar(2),datepart(hh,".$DataColumn."))";
                $res = "CONCAT(HOUR(".$DataColumn."))";
                break;
            case 'D'://日
            default :
                //$res = "CONVERT(nvarchar(10),".$DataColumn.")";
                $res = "day($DataColumn)";
                break;
        }
        return $res;
    }
    
    /**
     * @方法：处理一些日期常量的表达式,解析日期型的字符表达式
     * @param type $exp 表达式 Expression
     * @param type $arr 字段值 DataColumn
     * @return string
     */
    protected function setDateCodeExp($Expression,$DataColumn){
        switch ($Expression){
            case 'Y'://年份
                //$res = "CONVERT(nvarchar(4),YEAR(".$DataColumn."))";
                $res = "CONCAT(year(".$DataColumn."),'年')";
                break;
            case 'YM'://年月
                //$res = "CONVERT(nvarchar(4),YEAR(".$DataColumn."))+'-'+RIGHT('00'+CAST(MONTH(".$DataColumn.") AS VARCHAR(2)),2)";
                $res = "CONCAT(year(".$DataColumn."),'年',month(".$DataColumn."),'月')";
                break;
            case 'YW'://年周
                //$res = "CONVERT(varchar(10),DATEADD(wk, DATEDIFF(wk,0,DateAdd(dd,-1,".$DataColumn.")),0),120)";//周一作为周的第一天,主要注意周日要减一天
                //$res .= "+'|'+";
                //$res .= "CONVERT(varchar(10),DATEADD(wk, DATEDIFF(wk,0,DateAdd(dd,-1,".$DataColumn.")),6),120)";//周日作为周的最后一天

                $res1 = "date_sub(".$DataColumn.",INTERVAL WEEKDAY(".$DataColumn.") + 1 DAY)";
                $res2 = "date_sub(".$DataColumn.",INTERVAL WEEKDAY(".$DataColumn.") - 5 DAY)";
                
                $res = "CONCAT(".$res1.",'|',".$res2.")";
                
                break;
            case 'YQ'://年季
                //$res = "CONVERT(nvarchar(4),YEAR(".$DataColumn."))+'-'+CONVERT(nvarchar(2),DATEPART(QQ,".$DataColumn."))";
                $res = "CONCAT(year(".$DataColumn."),'-',QUARTER(".$DataColumn."))";
                break;
            case 'YB'://半年
                //$res = "CONVERT(nvarchar(4),YEAR(".$DataColumn."))+'-'+(case when MONTH(".$DataColumn.")<=6 then '1' else '2' end)";
                $res = "CONCAT(year(".$DataColumn."),'-',(case when MONTH(".$DataColumn.")<=6 then '1' else '2' end))";
                break;
            case 'HH'://小时24小时制
                //$res = "CONVERT(nvarchar(2),datepart(hh,".$DataColumn."))";
                $res = "CONCAT(hour(".$DataColumn."))";
                break;
            case 'D'://日
            default :
                $res = "day($DataColumn)";
                break;
        }
        return $res;
    }
    
    
    /**
     * @解析区间段表达式
     * @param type $column
     * @param type $regArr
     * @return string
     */
    protected function setNumberExp($column,$regArr){        
        $resString='(case ';
       
        foreach ($regArr as $key=>$val){
            if($val['i']==0&&$val['a']>0){
                $resString.=' when '.$column.'<='.$val['a'].' then '.$key;
            }
            if($val['i']>0&&$val['a']==0){
                $resString.=' when '.$column.'>'.$val['i'].' then '.$key;
            }
            if($val['i']>0&&$val['a']>0){
                $resString.=' when '.$column.'>'.$val['i'].' and '.$column.'<='.$val['a'].' then '.$key;
            }
        }
        
        $resString.=' end)';

        return $resString;
    }
    
    
    /**
     * *************************************************************************
     * 以上为初始化配置程序
     * *************************************************************************
     */
    
    /**
     * @返回表达式
     * @param type $Expression
     * @return type
     */
    private function setSelectExpressionColumn($Expression){
        return $Expression;
    }
    
    /**
     * @解析字段表达式：支持加、减、乘、除简单表达式
     * @param type $DataColumn
     * @return type
     */
    private function setSelectDataColumn($DataColumn){
        if(strpos($DataColumn,'/')){
            $arr=  explode('/', $DataColumn);
         
            $k0 = $this->AxisConfig[$arr[0]]['DataColumn'];
            $k1 = $this->AxisConfig[$arr[1]]['DataColumn'];
            
            $column =  '(case when '.$k0.'/'.$k1.'>0'.' then '.$k0.'/'.$k1.' else 0 end)';
        }elseif(strpos($DataColumn,'*')){
            $arr=  explode('*', $DataColumn);
            $k0 = $this->AxisConfig[$arr[0]]['DataColumn'];
            $k1 = $this->AxisConfig[$arr[1]]['DataColumn'];
            
            $column =  $k0.'*'.$k1;
        }elseif(strpos($DataColumn,'+')){
            $arr=  explode('+', $DataColumn);
            $k0 = $this->AxisConfig[$arr[0]]['DataColumn'];
            $k1 = $this->AxisConfig[$arr[1]]['DataColumn'];
            
            $column =  $k0.'+'.$k1;
        }elseif(strpos($DataColumn,'-')){
            $arr=  explode('-', $DataColumn);
            $k0 = $this->AxisConfig[$arr[0]]['DataColumn'];
            $k1 = $this->AxisConfig[$arr[1]]['DataColumn'];
            
            $column =  $k0.'-'.$k1;
        }else{
            $column = $DataColumn;
        }
        return $column;
    }

    private function parseDataColumnRateExp($value){
        if(is_array($value)){
            if($value['FieldRate'] < 1){
                $colums = $value['FieldOut'].'*'.$value['FieldRate'];
            }else{
                $colums = $value['FieldOut'];
            }            
        }else{
            $colums = $value;
        }
        return $colums;
    }

    /**
     * @方法：解析SELECT字段的表达式
     * @param type $key
     * @param type $value此处为数组，主要是用于存在特殊表达式时
     * @return string
     */
    private function parseDataColumnSelectExp($key,$value){

        switch ($this->AxisConfig[$key]['FieldType']){
            case 2://日期型
                if(empty($value)){
                    $selectColumn = $this->setDateExp($this->AxisConfig[$key]['Expression'],$this->AxisConfig[$key]['DataColumn']).' as '.$key;
                }else{
                    $selectColumn = $value.' as '.$key;
                }                
                break;
            case 3://数值型
       
                if(empty($value)){
                    $Expression = $this->AxisConfig[$key]['Expression'];
                   
                    if(!empty($Expression)){//优先考虑表达式
                        $column = $this->setSelectExpressionColumn($Expression);
                    }else{
                        $column = $this->setSelectDataColumn($this->AxisConfig[$key]['DataColumn']);
                    }
                    $selectColumn = $this->setNumberExp($column,$this->regionColumn[$key]).' as '.$key;
                    
                }else{
                    $selectColumn = $value.' as '.$key;
                }
                break;
            case 1://对照关系时
            case 4://文本
            default :
                if(empty($value)){
                    $selectColumn = $this->AxisConfig[$key]['DataColumn'].' as '.$key;
                }else{
                    $selectColumn = $value.' as '.$key;
                }                
                break;
        }
        return $selectColumn;
    }

        /**
     * @解析最原始的字段表达式
     * @return type
     */
    private function parseSelectOrigin($Axis){
        //初始化特殊值:此处无
        $value = '';
        //生成表达式
        foreach ($Axis as $key){
            $resArr[] = $this->parseDataColumnSelectExp($key,$value);
        }
        return implode(',', $resArr);
    }

    
    /**
     * @解析带有特殊处理的字段表达式:需要先判断特殊字段是否在统计维度当中
     * @param type $FieldCode
     * @param type $Special
     * @return type
     */
    private function parseSpecialSelectOrigin($Axis,$Special){
        $FieldCode = $Special['FieldCode'];
        $FieldOut = $Special['FieldOut'];

        foreach ($Axis as $key){
            if($key == $FieldCode){
                $resArr[] = $this->parseDataColumnSelectExp($key,$FieldOut);
            }else{
                $resArr[] = $this->parseDataColumnSelectExp($key,'');
            }            
        }
        return implode(',', $resArr);
    }
    
    /**
     * @处理特殊字段表达式
     * @param type $param
     * @return string
     */
    private function parseSpecialWhere($param=array()){
        $whereArrar = array_merge($this->condition,$param);
        
        $condition = $this->parseWhereCondition($whereArrar);
        
        $where = implode(' and ', $condition);
        
        return $where;
    }
   

    /**
     * @解析数据源中的统计字段表达式：统计值原始取值
     * @param type $vAxis
     * @return type
     */
    private function parseCountOrigin($vAxis){

        foreach ($vAxis as $val){
            $Expression = $this->AxisConfig[$val['k']]['Expression'];
    
            if(!empty($Expression)){//有表达式---优先处理
                $resArr[] = $this->setSelectExpressionColumn($Expression).' as '.$this->prefix.$val['k'];
            }else{
                $DataColumn = $this->AxisConfig[$val['k']]['DataColumn'];
                
                if(strpos($DataColumn,'/')){
                    $arr = explode('/', $DataColumn);
                    $k1 = strtoupper($arr[0]);
                    $k2 = strtoupper($arr[1]);
                    
                    //当字段也存在表达式时
                    if(!empty($this->AxisConfig[$k1]['Expression'])){
                        $resArr[] = $this->setSelectExpressionColumn($this->AxisConfig[$k1]['Expression']).' as '.$this->prefix.$k1;
                    }else{//不存在特殊表达式时
                        $resArr[] = $this->AxisConfig[$k1]['DataColumn'].' as '.$this->prefix.$k1;
                    }
                    if($this->AxisConfig[$k2]['Expression']){
                        $resArr[] = $this->setSelectExpressionColumn($this->AxisConfig[$k2]['Expression']).' as '.$this->prefix.$k2;
                    }else{
                        $resArr[] = $this->AxisConfig[$k2]['DataColumn'].' as '.$this->prefix.$k2;
                    }
                    
                    if(strtoupper($val['c'])=='MIN'||strtoupper($val['c'])=='MAX'){
                        if(!empty($this->AxisConfig[$k1]['Expression'])){
                            $c1 = $this->setSelectExpressionColumn($this->AxisConfig[$k1]['Expression']);
                        }else{//不存在特殊表达式时
                            $c1 = $this->AxisConfig[$k1]['DataColumn'];
                        }
                        if(!empty($this->AxisConfig[$k2]['Expression'])){
                            $c2 = $this->setSelectExpressionColumn($this->AxisConfig[$k2]['Expression']);
                        }else{//不存在特殊表达式时
                            $c2 = $this->AxisConfig[$k2]['DataColumn'];
                        }
                        $resArr[] = 'cast((case when '.$c2.'>0 then '.$c1.'/'.$c2.' else 0 end) as decimal(10,2))'.' as '.$this->prefix.$val['k'];
                    }
                }elseif(strpos($DataColumn,'*')){
                    $arr = explode('*', $DataColumn);
                    $k1 = strtoupper($arr[0]);
                    $k2 = strtoupper($arr[1]);
                    
                    //当字段也存在表达式时
                    if(!empty($this->AxisConfig[$k1]['Expression'])){
                        $c1 = $this->setSelectExpressionColumn($this->AxisConfig[$k1]['Expression']);
                    }else{//不存在特殊表达式时
                        $c1 = $this->AxisConfig[$k1]['DataColumn'];
                    }
                    if(!empty($this->AxisConfig[$k2]['Expression'])){
                        $c2 = $this->setSelectExpressionColumn($this->AxisConfig[$k2]['Expression']);
                    }else{//不存在特殊表达式时
                        $c2 = $this->AxisConfig[$k2]['DataColumn'];
                    }
                    $resArr[] = $c1.'*'.$c2.' as '.$this->prefix.$val['k'];
                }elseif(strpos($DataColumn,'+')){
                    $arr = explode('+', $DataColumn);
                    $k1 = strtoupper($arr[0]);
                    $k2 = strtoupper($arr[1]);
                    //当字段也存在表达式时
                    if(!empty($this->AxisConfig[$k1]['Expression'])){
                        $c1 = $this->setSelectExpressionColumn($this->AxisConfig[$k1]['Expression']);
                    }else{//不存在特殊表达式时
                        $c1 = $this->AxisConfig[$k1]['DataColumn'];
                    }
                    if(!empty($this->AxisConfig[$k2]['Expression'])){
                        $c2 = $this->setSelectExpressionColumn($this->AxisConfig[$k2]['Expression']);
                    }else{//不存在特殊表达式时
                        $c2 = $this->AxisConfig[$k2]['DataColumn'];
                    }
                    $resArr[] = $c1.'+'.$c2.' as '.$this->prefix.$val['k'];
                }elseif(strpos($DataColumn,'-')){
                    $arr = explode('-', $DataColumn);
                    $k1 = strtoupper($arr[0]);
                    $k2 = strtoupper($arr[1]);
                    //当字段也存在表达式时
                    if(!empty($this->AxisConfig[$k1]['Expression'])){
                        $c1 = $this->setSelectExpressionColumn($this->AxisConfig[$k1]['Expression']);
                    }else{//不存在特殊表达式时
                        $c1 = $this->AxisConfig[$k1]['DataColumn'];
                    }
                    if(!empty($this->AxisConfig[$k2]['Expression'])){
                        $c2 = $this->setSelectExpressionColumn($this->AxisConfig[$k2]['Expression']);
                    }else{//不存在特殊表达式时
                        $c2 = $this->AxisConfig[$k2]['DataColumn'];
                    }
                    $resArr[] = $c1.'-'.$c2.' as '.$this->prefix.$val['k'];
                }else{
                    $resArr[] = $DataColumn.' as '.$this->prefix.$val['k'];                    
                }
            }
        }
        //去重
        $resArr = array_unique($resArr);
        return implode(',', $resArr);
    }
    
    /**
     * @方法：将字段表达式，根据换算率解析
     * @param type $DataColumn
     * @param type $rate
     */
    private function setCountExpColumn($DataColumn,$rate){
        if($rate<1){
            $col = $DataColumn.'*'.$rate;
        }else{
            $col = $DataColumn;
        }
        return $col;
    }

    /**
     * @解析数据源中的统计字段表达式：统计值原始取值
     * @param type $vAxis
     * @return type
     */
    private function parseSpecialCountOrigin($vAxis,$rate){

        foreach ($vAxis as $val){
            $Expression = $this->AxisConfig[$val['k']]['Expression'];
    
            if(!empty($Expression)){//有表达式---优先处理
                $resArr[] = $this->setSelectExpressionColumn($Expression).' as '.$this->prefix.$val['k'];
            }else{
                $DataColumn = $this->AxisConfig[$val['k']]['DataColumn'];
                $preColumn = $this->prefix.$val['k'];//转义后的字段名
                
                if(strpos($DataColumn,'/')){
                    $arr = explode('/', $DataColumn);
                    $k1 = strtoupper($arr[0]);
                    $k2 = strtoupper($arr[1]);
                    
                    //当字段也存在表达式时
                    if(!empty($this->AxisConfig[$k1]['Expression'])){
                        $resArr[] = $this->setSelectExpressionColumn($this->AxisConfig[$k1]['Expression']).' as '.$this->prefix.$k1;
                    }else{//不存在特殊表达式时
                        $resArr[] = $this->setCountExpColumn($this->AxisConfig[$k1]['DataColumn'],$rate).' as '.$this->prefix.$k1;
                    }
                    if($this->AxisConfig[$k2]['Expression']){
                        $resArr[] = $this->setSelectExpressionColumn($this->AxisConfig[$k2]['Expression']).' as '.$this->prefix.$k2;
                    }else{
                        $resArr[] = $this->setCountExpColumn($this->AxisConfig[$k2]['DataColumn'],$rate).' as '.$this->prefix.$k2;
                    }
                    
                    if(strtoupper($val['c'])=='MIN'||strtoupper($val['c'])=='MAX'){
                        if(!empty($this->AxisConfig[$k1]['Expression'])){
                            $c1 = $this->setSelectExpressionColumn($this->AxisConfig[$k1]['Expression']);
                        }else{//不存在特殊表达式时
                            $c1 = $this->AxisConfig[$k1]['DataColumn'];
                        }
                        if(!empty($this->AxisConfig[$k2]['Expression'])){
                            $c2 = $this->setSelectExpressionColumn($this->AxisConfig[$k2]['Expression']);
                        }else{//不存在特殊表达式时
                            $c2 = $this->AxisConfig[$k2]['DataColumn'];
                        }
                        $resArr[] = 'cast((case when '.$c2.'>0 then '.$c1.'/'.$c2.' else 0 end) as decimal(10,2))'.' as '.$this->prefix.$val['k'];
                    }
                }elseif(strpos($DataColumn,'*')){
                    $arr = explode('*', $DataColumn);
                    $k1 = strtoupper($arr[0]);
                    $k2 = strtoupper($arr[1]);
                    
                    //当字段也存在表达式时
                    if(!empty($this->AxisConfig[$k1]['Expression'])){
                        $c1 = $this->setSelectExpressionColumn($this->AxisConfig[$k1]['Expression']);
                    }else{//不存在特殊表达式时
                        $c1 = $this->AxisConfig[$k1]['DataColumn'];
                    }
                    if(!empty($this->AxisConfig[$k2]['Expression'])){
                        $c2 = $this->setSelectExpressionColumn($this->AxisConfig[$k2]['Expression']);
                    }else{//不存在特殊表达式时
                        $c2 = $this->AxisConfig[$k2]['DataColumn'];
                    }
                    $resArr[] = $this->setCountExpColumn($c1.'*'.$c2,$rate).' as '.$this->prefix.$val['k'];
                }elseif(strpos($DataColumn,'+')){
                    $arr = explode('+', $DataColumn);
                    $k1 = strtoupper($arr[0]);
                    $k2 = strtoupper($arr[1]);
                    //当字段也存在表达式时
                    if(!empty($this->AxisConfig[$k1]['Expression'])){
                        $c1 = $this->setSelectExpressionColumn($this->AxisConfig[$k1]['Expression']);
                    }else{//不存在特殊表达式时
                        $c1 = $this->AxisConfig[$k1]['DataColumn'];
                    }
                    if(!empty($this->AxisConfig[$k2]['Expression'])){
                        $c2 = $this->setSelectExpressionColumn($this->AxisConfig[$k2]['Expression']);
                    }else{//不存在特殊表达式时
                        $c2 = $this->AxisConfig[$k2]['DataColumn'];
                    }
                    $resArr[] = $this->setCountExpColumn($c1.'+'.$c2,$rate).' as '.$this->prefix.$val['k'];
                }elseif(strpos($DataColumn,'-')){
                    $arr = explode('-', $DataColumn);
                    $k1 = strtoupper($arr[0]);
                    $k2 = strtoupper($arr[1]);
                    //当字段也存在表达式时
                    if(!empty($this->AxisConfig[$k1]['Expression'])){
                        $c1 = $this->setSelectExpressionColumn($this->AxisConfig[$k1]['Expression']);
                    }else{//不存在特殊表达式时
                        $c1 = $this->AxisConfig[$k1]['DataColumn'];
                    }
                    if(!empty($this->AxisConfig[$k2]['Expression'])){
                        $c2 = $this->setSelectExpressionColumn($this->AxisConfig[$k2]['Expression']);
                    }else{//不存在特殊表达式时
                        $c2 = $this->AxisConfig[$k2]['DataColumn'];
                    }
                    $resArr[] = $this->setCountExpColumn($c1.'-'.$c2,$rate).' as '.$this->prefix.$val['k'];
                }else{
                    $resArr[] = $this->setCountExpColumn($DataColumn,$rate).' as '.$this->prefix.$val['k'];
                }
            }
        }
        //去重
        $resArr = array_unique($resArr);
        return implode(',', $resArr);
    }

    /**
     * @方法：解析含有特殊表达式的SQL
     * @param type $allAxis表示特殊计算字段维度
     * @param type $sAxis表示特殊计算时，X维度数组
     * @param type $vAxis
     * @return string
     */
    private function getSpecialDataSelectOrigin($allAxis,$vAxis,$sAxis=array()){
        $OriginData = array();
        //处理：当计算特殊字段表达时，需要计算占比时，计算字段
        if(empty($sAxis)){
            $Axis=$allAxis;
        }else{
            $Axis=$sAxis;
        }
        
        foreach ($this->BdataSpecial as $val){
            if(in_array($val['FieldCode'], $allAxis)){
                $condition = $val['FieldColumn'];

                $param = array($condition=>array('r'=>$val['FieldExp'],'v'=>$val['FieldValue']));
                
                $cWhere = $this->parseSpecialWhere($param);
                
                $OriginData[] = 'select '
                                .$this->parseSpecialSelectOrigin($Axis,$val)
                                .','
                                .$this->parseSpecialCountOrigin($vAxis,$val['FieldRate'])
                                .' from '
                                .$this->DataBase
                                .' where '
                                .$cWhere; 
            }
        }
        return $OriginData;
    }

    /**
     * *************************************************************************
     * 以上为解析原始数据读取
     * *************************************************************************
     */
    /**
     * @方法：最终统计字段表达式
     * @param type $key
     * @param type $count
     * @return type
     */
    private function setColumnResultFormat($key,$count){
        return $key.'_'.$count;
    }
    
    /**
     * @方法：设置统计字段统计时表达式
     * @param type $val
     * @return string
     */
    private function setCountExpression($val){
        //初始化参数
        $key = $val['k'];
        $count = $val['c'];
        $uom = $val['u'];
        //计量单位
        //当汇总统计时，需要处理转换率，其他方式时，无需转换率
        if($count=='SUM'){
         
            if((int)$this->UomConfig[$uom]['IsMain'] > 0){//默认计量时，换算率为1，并忽略
                $rate = '';
            }else{
                $rate = '/'.$this->UomConfig[$uom]['Rate'];
            } 
        }else{
            $rate = '';
        }
        //字段表达式
        if(!empty($this->AxisConfig[$key]['Expression'])){//存在表达式
            $res = $count.'('.$this->prefix.$key.')'.$rate.' as '.$this->setColumnResultFormat($key,$count);
        }else{
            $DataColumn = $this->AxisConfig[$key]['DataColumn'];
          
            if(strpos($DataColumn,'/')){
                $arr = explode('/', $DataColumn);
               
                $k1 = $this->prefix.$arr[0];
                $k2 = $this->prefix.$arr[1];
                
                if($count=='AVG'){
                    
                        $le = 'SUM'.'('.$k1.')'.'/'.'SUM'.'('.$k2.')'.$rate;
                    
                }else{
                    $le = $count.'('.$this->prefix.$key.')'.$rate;
                }                
            }elseif(strpos($DataColumn,'*')){
                $arr = explode('*', $DataColumn);
                $a = strtoupper($arr[0]);
                $b = strtoupper($arr[1]);
                
                $k1 = $this->prefix.$this->AxisConfig[$a]['DataColumn'];
                $k2 = $this->prefix.$this->AxisConfig[$b]['DataColumn'];
                
                if($count=='SUM'){
                    $le = $count.'('.$k1.')'.'*'.$count.'('.$k2.')'.$rate;
                    
                }else{
                    $le = $count.'('.$this->prefix.$key.')'.$rate;
                } 
            }elseif(strpos($DataColumn,'+')){
                $arr = explode('+', $DataColumn);
                $a = strtoupper($arr[0]);
                $b = strtoupper($arr[1]);
                
                $k1 = $this->prefix.$this->AxisConfig[$a]['DataColumn'];
                $k2 = $this->prefix.$this->AxisConfig[$b]['DataColumn'];
                
                if($count=='SUM'){
                    $le = '('.$count.'('.$k1.')'.'+'.$count.'('.$k2.')'.')'.$rate;
                }else{
                    $le = $count.'('.$this->prefix.$key.')'.$rate;
                }
            }elseif(strpos($DataColumn,'-')){
                $arr = explode('-', $DataColumn);
            
                $a = strtoupper($arr[0]);
                $b = strtoupper($arr[1]);
                $k1 = $this->prefix.$this->AxisConfig[$a]['DataColumn'];
                $k2 = $this->prefix.$this->AxisConfig[$b]['DataColumn'];
                 
                if($count=='SUM'){
                    $le = '('.$count.'('.$k1.')'.'-'.$count.'('.$k2.')'.')'.$rate;
                }elseif($count=='AVG'){
                    
                        $le = 'SUM'.'('.$k1.')'.'/'.'SUM'.'('.$k2.')'.$rate;
                    
                }else{
                    $le = $count.'('.$this->prefix.$key.')'.$rate;
                }
            }else{
                $le = $count.'('.$this->prefix.$key.')'.$rate;
            }
            
            $res = $le.' as '.$this->setColumnResultFormat($key,$count);
        }
        return $res;
    }
    
    /**
     * @方法：解析统计时，非统计字段的表达式
     * @return type
     */
    private function parseSelectColumnExp($Axis){
        return implode(',', $Axis);
    }

    /**
     * @方法：解析统计字段的表达式
     * @return type
     */
    private function parseCountColumnExp($vAxis){
        $res = array();
      
        foreach($vAxis as $val){
            if($val['c']=='RATE'){
                $key = $val['k'];
                if(!empty($this->AxisConfig[$key]['DataColumn'])){
                   
                    $totalSql = $this->setBdataRateSql($key);
                    $res[] = $this->setRateCount($key,$totalSql);
                }
            }else{
                $res[] = $this->setCountExpression($val);
                
            }
        }
        return implode(',', $res);
    }
    
    /**
     * @获取统计字段最终呈现的表达式
     * @param type $vAxis
     * @return type
     */
    private function parseCountResultColumnExp($vAxis){
        $res = array();
       
        foreach($vAxis as $val){
            $res[] = $this->setColumnResultFormat($val['k'],$val['c']);
        }
        return implode(',', $res);
    }
    
    
    /**
     * @将占比结果解析成字段表达式
     * @param type $column
     * @param type $total
     */
    private function setRateCount($key,$totalSql){
        $column = $this->prefix.$this->AxisConfig[$key]['DataColumn'];
        return '100*sum'.'('.$column.')'.'/'.'('.$totalSql.')'.' as '.$this->setColumnResultFormat($key,'RATE');
    }
    
    /**
     * @处理占比时，合计数据源与主表数据的WHERE关系
     * @param type $left 当前表别名
     * @param type $right 主表别名
     * @param type $Axis 维度值
     * @return type
     */
    private function setBdataGX($left,$right,$Axis){
        $res = array();
        foreach($Axis as $key){
            $res[] = $left.'.'.$key.'='.$right.'.'.$key;
        }
        return implode(' and ', $res);
    }
    
    /**
     * @此方法仅用于含有占比计算的统计方法时
     * @param type $key
     * @return string
     */
    private function setBdataRateSql($key){
        $where = (empty($this->where))?' ':' where '.$this->where;
        if($this->xNums > 0){//存在横向坐标时
            if($this->yNums>0){//存在纵向坐标
                //构造V数组
                $vAxis[] = array('k'=>$key,'c'=>'SUM','u'=>$this->AxisConfig[$key]['Uom']);
                //原始表
                $sql0 = 'select '
                        .$this->parseSelectOrigin($this->xAxis)
                        .','
                        .$this->parseCountOrigin($vAxis)
                        .' from '
                        .$this->DataBase
                        .$where;

                //特殊数据处理            
                //$this->xAxis表示计算占比总和时所需要的字段
                $SpecialArr = $this->getSpecialDataSelectOrigin($this->yAxis,$vAxis,$this->xAxis);
                if(!empty($SpecialArr)){
                    $sql0 .= ' union all '.implode(' union all ', $SpecialArr);
                }
                
                //统计数据
                $sql3 = 'select '
                        .$this->parseSelectColumnExp($this->xAxis)
                        .','
                        .$this->parseCountColumnExp($vAxis)
                        .' from '
                        .'('.$sql0.')'.' as aSQL'
                        .' group by '.$this->parseSelectColumnExp($this->xAxis);
                
                //与主表关联
                $sql = 'select '.$this->setColumnResultFormat($key, 'SUM')
                        .' from '.'('.$sql3.')'.' as RT'
                        .' where '.$this->setBdataGX('RT','OT',$this->xAxis);
                
            }else{//有横坐标但无纵向坐标
                $sql = 'select '
                        .'sum('
                        .$this->AxisConfig[$key]['DataColumn']
                        .')'
                        .' as Total'
                        .' from '
                        .$this->DataBase
                        .$where;
            }
        }else{//不存在横坐标，但必有纵坐标
            $sql = 'select '
                    .'sum('.$this->AxisConfig[$key]['DataColumn'].')'
                    .' as Total'
                    .' from '
                    .$this->DataBase
                    .$where;
        }
        
        return $sql;
    }
    
    
     /**
     * @解析数据源读取的SQL，反馈统计过的数据SQL
     * @return string
     */
     private function getDataSelectOrigin(){
        //0.初始化配置表：获取数据源信息
        $this->getBaseDataOrigin();

        //1.基础表数据读取
        if($this->DataType == 'STORED'){
            $where = '';
        }else{
            $where = (empty($this->where))?' ':' where '.$this->where;
        }
    
        $sql='select '
             .$this->parseSelectOrigin($this->allAxis)
             .','
             .$this->parseCountOrigin($this->vAxis)
             .' from '
             .$this->DataBase
             .$where;
        //2.表示存在特殊计算字段:计算特殊表达式的数据读取
        $SpecialArr = $this->getSpecialDataSelectOrigin($this->allAxis,$this->vAxis,array());
        
        if(!empty($SpecialArr)){
            $sql .= ' union all '.implode(' union all ', $SpecialArr);
        }
        
        //3.计算统计结果
        $result = 'select '
                  .$this->parseSelectColumnExp($this->allAxis)
                  .','
                  .$this->parseCountColumnExp($this->vAxis)
                  .' from '
                  .' ('.$sql.')'.' as OT'
                  .' group by '.$this->parseSelectColumnExp($this->allAxis);

        
        return $result;
        
    }

    
    /*
     * *************************************************************************
     * 以上为数据源读取部分
     * *************************************************************************
     */
    
    /**
     * @将维度数组传入，解析成带中文解析所需要的表达式
     * @param type $Axis
     * @return type
     */
    private function getInnerData($Axis){
        $i=0;
        foreach ($Axis as $key){
           
            $tb='tb'.$i;
            switch ($this->AxisConfig[$key]['FieldType']){
                case 1:
                    $select[] = $key;//ID字段
                    
                    $select[] = $tb.'.'.$this->AxisConfig[$key]['ContrastName'].' as '.'T_'.$key;
                    $inner[] = 'inner join '.$this->AxisConfig[$key]['ContrastSheet'].' as '.$tb.' on '.$tb.'.'.$this->AxisConfig[$key]['ContrastId'].'='.$key;
                    $order[] = $tb.'.'.$this->AxisConfig[$key]['ContrastSort'];
                    break;
                case 2:
                case 3:
                case 4:
                default :
                    $select[] = $key;//显示字段
                    $order[] = $key;
                    break;
            }
            ++$i;
        }
        
        $result['select'] = implode(',', $select);
        
        $result['inner'] = implode(' ', $inner);
        $result['order'] = implode(',', $order);
        
        return $result;
    }

    /*
     * *************************************************************************
     * 以上为读取数据的中文名称[公共部分]
     * *************************************************************************
     */
    
    /**
     * @解析统计字段显示名称
     * @param type $val
     * @return type
     */
    protected function parseCountName($value){
        
        $FieldName = $this->AxisConfig[$value['k']]['FieldName'];
        $CountName = $this->CountConfig[$value['c']]['CountName'];
        
        //计量单位
        switch ($value['c']){
            case 'AVG':
            case 'MAX';
            case 'MIN';
                $uom = $this->AxisConfig[$value['k']]['Uom'];
                $UomName = $this->UomConfig[$uom]['UomName'];
                break;
            case 'RATE':
                $UomName = '%';
                break;
            case 'SUM':
            default :
                $UomName = $this->UomConfig[$value['u']]['UomName'];
                break;
        }
        
        return $FieldName.'('.$CountName.','.$UomName.')';
    }
    
    /*
     * *************************************************************************
     * 以上为解析各种字段表达式
     * *************************************************************************
     */
    
    /**
     * @解析排名分析时，表头数组
     * @此时的逻辑：表头的f需要影响行的f，i影响着行的精度
     * @方法内参数意义说明：$field表示字段代号（需要头与体一致；）$id表示字段标志，与数据库读取结果一致
     * @return type数组
     * $this->Header用于记录表头数组值
     */
    private function getToppicHead(){
        //解析左侧表头数据
        $i=0;
        foreach ($this->yAxis as $key){
            if($this->yNums > 1){//多排序维度
                if($this->yNums - $i == 1){//最后一列插入排序序号列
                    $head[0][] = array('f'=>'rank','i'=>'rank','t'=>$this->AxisConfig['rank']['FieldName']);
                    $this->Header['rank'] = array('p'=>'L');//P表示位置：当为L时表示为左侧表头，不要精度
                }
            }else{
                if($i == 0){//首页插入排序列
                    $head[0][] = array('f'=>'rank','i'=>'rank','t'=>$this->AxisConfig['rank']['FieldName']);
                    $this->Header['rank'] = array('p'=>'L');//P表示位置：当为L时表示为左侧表头，不要精度
                }
            }
            
            $head[0][] = array('f'=>$key,'i'=>$key,'t'=>$this->AxisConfig[$key]['FieldName']);
            $this->Header[$key] = array('p'=>'L','t'=>$this->AxisConfig[$key]['FieldType']);//P表示位置：当为L时表示为左侧表头，不要精度;t表示字段类型
            
            ++$i;
        }
        
        //解析表头统计字段表达式
        $j=0;
        foreach ($this->vAxis as $val){
            $id = $this->setColumnResultFormat($val['k'],$val['c']);
            $field = 'B'.$j;            
            $title = $this->parseCountName($val);
            
            //解析字段的数据精度
            $Accuracy = $this->AxisConfig[$val['k']]['Accuracy'];
            
            $head[0][] = array('f'=>$field,'i'=>$id,'t'=>$title);
            $this->Header[$id] = array('p'=>'M','f'=>$field,'a'=>$Accuracy);//P表示位置：当为M时表示为中间表头，需要精度
            
            ++$j;
        }
        
        return $head;
    }

    /**
     * @解析当无X维度时，表头数组
     * @return string
     */
    private function getNoxAxisHead(){
        //解析左侧表头数据
        foreach ($this->yAxis as $key){
            $head[0][] = array('f'=>$key,'i'=>$key,'t'=>$this->AxisConfig[$key]['FieldName']);
            $this->Header[$key] = array('p'=>'L','t'=>$this->AxisConfig[$key]['FieldType']);//P表示位置：当为L时表示为左侧表头，不要精度;t表示字段类型
        }
        
        //解析表头统计字段表达式
        $j=0;
        foreach ($this->vAxis as $val){
            $id = $this->setColumnResultFormat($val['k'],$val['c']);
            $field = 'B'.$j;
            $title = $this->parseCountName($val);
            
            //解析字段的数据精度
            $Accuracy = $this->AxisConfig[$val['k']]['Accuracy'];
            
            $head[0][] = array('f'=>$field,'i'=>$id,'t'=>$title);
            $this->Header[$id] = array('p'=>'M','f'=>$field,'a'=>$Accuracy);//P表示位置：当为M时表示为中间表头，需要精度
            $this->HeaderConfig[$id] = $field;//设置代号与字段的对照关系
            
            ++$j;
        }
        
        return $head;
    }
    
    
    /**
     * @解析当无Y维度时，表头数组
     * @return string
     */
    private function getNoyAxisHead(){
        if($this->xNums>1){//多层表头
            for($i=0;$i<$this->xNums;$i++){
                //左侧
                $head[$i][] = array('i'=>'yAxis','f'=>'yAxis','t'=>$this->AxisConfig['yAxis']['FieldName']);
                //中间
                if($i == $this->xNums - 1){//最后一层
                    foreach ($this->Header[$i] as $val){
                        $head[$i][] = $val;
                    }
                }else{//非最后一层
                    $hLines = array_count_values($this->Header[$i]);
                    foreach($hLines as $key=>$nums){
                        $arr = explode('@', $key);
                        $count = count($arr);
                        $head[$i][] = array('t'=>$arr[$count-1],'c'=>$nums);
                    }
                }
                
                //需要右合计时
                if($this->count['isXSum']>0){
                    $head[$i][] = array('i'=>'gTotal','f'=>'gTotal','t'=>$this->AxisConfig['gTotal']['FieldName']);
                }
            }
            
        }else{//单个表头时
            //左侧
            $head[0][] = array('i'=>'yAxis','f'=>'yAxis','t'=>$this->AxisConfig['yAxis']['FieldName']);
            //中间
            foreach ($this->Header[0] as $val){
                $head[0][] = $val;
            }
            //需要右合计时
            if($this->count['isXSum']>0){
                $head[0][] = array('i'=>'gTotal','f'=>'gTotal','t'=>$this->AxisConfig['gTotal']['FieldName']);
            }
        }
      
        return $head;
    }
    
    
    /**
     * @解析XYV维度时，表头数组
     * @数据源为$this->Header
     * @return string
     */
    private function getXYVAxisHead(){
        $head = array();
        $right = array();
        
        //左侧表头
        for($i=0;$i<$this->levelNums;$i++){
            foreach ($this->yAxis as $key){
                if($i == $this->levelNums - 1){
                    $head[$i][] = array('f'=>$key,'i'=>$key,'t'=>$this->AxisConfig[$key]['FieldName']);
                }else{
                    $head[$i][] = array('f'=>$key,'t'=>$this->AxisConfig[$key]['FieldName']);
                }                
            } 
        }
        
        $i = 0;
                
        //数据表头
        if($this->levelNums>1){
            //取出表头数据。形成多层数组
            foreach ($this->Header as $key=>$val){
                $title = explode('@', $val);
                $t = array();
                
                for($j=0;$j<$this->levelNums;$j++){
                    if($j == $this->levelNums - 1){//最后一行
                        $field = 'B'.$i;
                        $name = $title[$j];
                        $head[$j][] = array('f'=>$field,'i'=>$key,'t'=>$name);
                        
                        $this->HeaderConfig[$key] = $field;
                        ++$i;
                    }else{//非最后一行
                        for($k=0;$k<=$j;$k++){
                            $t[] = $title[$k];
                        }
                        $headArr[$j][] = implode('@', $t);
                    }
                }
            }
            
            //对非末层进行去重处理
            for($k=0;$k < $this->levelNums-1;$k++){
                $hLines = array_count_values($headArr[$k]);
                
                foreach($hLines as $key=>$nums){
                    $arr = explode('@', $key);
                    $count = count($arr);
                    $head[$k][] = array('t'=>$arr[$count-1],'c'=>$nums);
                }
            }
            
            //需要横合计时
            if($this->count['isXSum']>0){
                for($kk=0;$kk < $this->levelNums;$kk++){
                    if($kk == $this->levelNums -1){
                        if($this->vNums>1){
                            foreach($this->vAxis as $val){
                                $field = $this->setColumnResultFormat($val['k'], $val['c']);
                                $name = $this->parseCountName($val);
                                $head[$kk][] = array('f'=>$field,'i'=>$field,'t'=>$name);
                            }
                        }else{
                            $field = 'gTotal';
                            $name = $this->AxisConfig[$field]['FieldName'];
                            $head[$kk][] = array('f'=>$field,'i'=>$field,'t'=>$name);
                        }
                    }else{
                        $head[$kk][] = array('f'=>'gTotal','i'=>'gTotal','t'=>$this->AxisConfig['gTotal']['FieldName'],'c'=>$this->vNums);
                    }
                }                            
            }
        }else{//单层
            foreach ($this->Header as $key=>$val){
                $field = 'B'.$i;
                $this->HeaderConfig[$key] = $field;
                $head[0][] = array('f'=>$field,'i'=>$key,'t'=>$val);
                ++$i;
            }
            
            //需要横合计时
            if($this->count['isXSum']>0){
                $head[0][] = array('f'=>'gTotal','i'=>'gTotal','t'=>$this->AxisConfig['gTotal']['FieldName']);
            }
        }
        
        return $head;
    }

    /*
     * *************************************************************************
     * 以上为解析表头结果数组-------------------表头
     * *************************************************************************
     */

    /**
     * @对每行数据进行解析，返回处理后的结果集---用于排名榜
     * @param type $rows
     * @return type
     */
    private function setToppicLine($head,$rows){
        //初始化行数据数组
        $line = array();
        //初始化行ID数组
        $id = array();

        foreach ($head as $key=>$val){                    
            switch ($val['p']){
                case 'M':
                    $line[$val['f']]=round($rows[$key],$val['a']);
                    break;
                case 'R':
                    $line[$key]=round($rows[$key],$val['a']);//此段需要调整
                    break;
                case 'L':
                default :
                    switch($val['t']){
                        case 1:
                            $field = 'T_'.$key;
                            $line[$key]=$rows[$field];
                            $id[] = $rows[$key];//ID值
                            break;
                        case 3:
                            $field = $rows[$key];
                            $line[$key]=$this->regionColumn[$key][$field]['t'];
                            $id[] = $this->regionColumn[$key][$field]['i'].'-'.$this->regionColumn[$key][$field]['a'];//ID值
                            break;
                        case 2:
                        case 4:
                            $line[$key]=$rows[$key];
                            $id[] = $rows[$key];
                            break;
                        default :
                            $line[$key]=$rows[$key];
                            break;
                    }
                    break;
            }
        }
        
        //字段若是y维度中的数据
        $line['id'] = implode('@', $id);
        
        return $line;
    }
    
    /**
     * @对每行数据进行解析，返回处理后的结果集---用于无X维度时的行数据处理
     * @param type $rows
     * @return type
     */
    private function setNoxAxisLine($head,$rows){
        //初始化行数据数组
        $line = array();
        
        //初始化行ID数组
        $id = array();

        //初始化是否需要表尾合计
        $isYSum = $this->count['isYSum'];
        
        //解析每行数据
        foreach ($head as $key=>$val){
            switch ($val['p']){
                case 'M':
                    if($isYSum > 0){//需要合计
                        $num = round($rows[$key],$val['a']); 
                        $line[$val['f']] = $num;
                        $this->Footer[$key][] = $num;//累计底部数值
                    }else{
                       $line[$val['f']]=round($rows[$key],$val['a']); 
                    }
                    break;
                case 'R':
                    $line[$key]=round($rows[$key],$val['a']);//此段需要调整
                    break;
                case 'L':
                default :
                    switch($val['t']){
                        case 1:
                            $field = 'T_'.$key;
                            $line[$key]=$rows[$field];
                            $id[] = $rows[$key];//ID值
                            break;
                        case 3:
                            $field = $rows[$key];
                            $line[$key]=$this->regionColumn[$key][$field]['t'];
                            $id[] = $this->regionColumn[$key][$field]['i'].'-'.$this->regionColumn[$key][$field]['a'];//ID值
                            break;
                        case 2:
                        case 4:
                            $line[$key]=$rows[$key];
                            $id[] = $rows[$key];
                            break;
                        default :
                            $line[$key]=$rows[$key];
                            break;
                    }
                    break;
            }
            ++$i;
        }
        
        //字段若是y维度中的数据
        $line['id'] = implode('@', $id);

        return $line;
    }
    
    /**
     * @方法：解析数据行，返回数据格式
     * @param type $data
     * @return type
     */
    private function getToppicRows($data){
        $result=array();
        foreach ($data as $rows){
            $result[] = $this->setToppicLine($this->Header,$rows);
        }
        return $result;
    }
    
    /**
     * @方法：解析数据行，返回数据格式---无X维度时
     * @param type $data
     * @return type
     */
    private function getNoxAxisRows($data){
        $result=array();
        foreach ($data as $rows){
            $result[] = $this->setNoxAxisLine($this->Header,$rows);
        }
        return $result;
    }
    
    
    /**
     * @方法：解析数据行，返回数据格式---无Y维度时
     * @param type $data
     * @return type
     */
    private function getNoyAxisRows($data){
        $result=array();

        //左侧数据表达式
        foreach($this->vAxis as $val){
            $key = $this->setColumnResultFormat($val['k'], $val['c']);
            $line[$key]['id'] = $key;//行标识
            $line[$key]['yAxis'] = $this->parseCountName($val);
        }
        
        $i=0;
        foreach($data as $key=>$val){
            //左
            $result[$i]['id'] = $line[$key]['id'];
            $result[$i]['yAxis'] = $line[$key]['yAxis'];
            //中
            foreach($val as $k=>$v){
                $field = $this->HeaderConfig[$k];
                $result[$i][$field] = $v;
            }

            // 需要右合计时
            if($this->count['isXSum']>0){
                $result[$i]['gTotal'] = $this->setTotalNums($key,$val);
            } ++$i;
        }
        
        return $result;
    }
    /*
     * *************************************************************************
     * 以上为解析表体数据的输出格式
     * *************************************************************************
     */
    /**
     * @根据数据统计合计
     * @param type $key
     * @return type
     */
    private function setTotalNums($key,$val){
        $array = explode('_', $key);
        $nums = 0;
        switch ($array[1]){
            case 'SUM':
            case 'RATE':
            default :
                $nums = array_sum($val);
                break;
            case 'AVG':            
                $nums = round(array_sum($val)/count($val),$this->AxisConfig[$array[0]]['Accuracy']);
                break;
            case 'MAX':
                $nums = max($val);
                break;
            case 'MIN':
                $nums = min($val);
                break;
        }
        return $nums;
    }


    /**
     * @方法：处理页脚合计值
     * @return type
     */
    private function getSingleFootData(){
        //左侧，合计所占列数
        if($this->yNums>1){
            $Footer[0][] = array('t'=>$this->AxisConfig['gTotal']['FieldName'],'f'=>$this->yAxis[0],'i'=>$this->yAxis[0],'c'=>$this->yNums);
        }else{
            $Footer[0][] = array('t'=>$this->AxisConfig['gTotal']['FieldName'],'f'=>$this->yAxis[0],'i'=>$this->yAxis[0]);
        }
        
        //中间数据处理
        foreach ($this->Footer as $key=>$val){
            $field = $this->HeaderConfig[$key];//字段代号
            
            $nums = $this->setTotalNums($key,$val);
            
            $Footer[0][] = array('t'=>$nums,'f'=>$field);
        }
        return $Footer;
    }


    /*
     * *************************************************************************
     * 以上为解析表尾数据的输出格式---------------------表尾
     * *************************************************************************
     */
    
    /**
     * @单个排序维度时，获取排名数据
     * @return string
     */
    private function getTopSingleAxisData(){
        $sql='';
        
        if(!empty($this->ranking)){
            //获取基础的数据源SQL
            $baseSQL = $this->getDataSelectOrigin();
            
            //初始化参数
            $topColumn = $this->setColumnResultFormat($this->ranking['topColumn'][0], $this->ranking['topColumn'][1]);
            $order = $this->ranking['topColumn'][2];
            
            $topNum = !empty($this->ranking['topNum'])?$this->ranking['topNum']:10;
            $orderby = $topColumn.' '.$order;             
            
            //解析排名数据源
            $countColumn = $this->parseCountResultColumnExp($this->vAxis);
            
            $orginSQL = 'select top '.$topNum
                        .' ROW_NUMBER() OVER(ORDER BY '.$orderby.') as rank'
                        .','
                        .$this->parseSelectColumnExp($this->yAxis)
                        .','
                        .$countColumn
                        .' from ('.$baseSQL.') as tp '
                        .' order by '.$orderby; 
            
            //解析数据的中文名称
            //排名时只有y维度
            $Inner = $this->getInnerData($this->yAxis);            
            
            $sql = 'select rank'
                   .','
                   .$Inner['select']
                    .','
                   .$countColumn
                   .' from ('.$orginSQL.') as ctp '
                   .$Inner['inner']
                   .' order by rank'; 
        }
	
        $result = M()->query($sql);
        
        return $result;
    }

    /**
     * @多排序维度时，获取排名数据
     * @return string
     */
    private function getTopMultiAxisData(){
        $sql = '';
        if(!empty($this->ranking)){
            //获取基础的数据源SQL
            $baseSQL = $this->getDataSelectOrigin();
            
            //初始化参数
            $topColumn = $this->setColumnResultFormat($this->ranking['topColumn'][0], $this->ranking['topColumn'][1]);
            $order = $this->ranking['topColumn'][2];
            
            $topNum = !empty($this->ranking['topNum'])?$this->ranking['topNum']:10;        
            $orderby = $topColumn.' '.$order;  
            
            //设置数据源别名
            $tb = 'rankTB';
            
            //解析y维度数组，解析成排名维度字符串
            $col=array();
            $i=1;
            foreach ($this->yAxis as $key){
                if($i < $this->yNums){
                   $col[]=$tb.'.'.$key; 
                   //用于结果排序依据
                   $rOrder[]=$key;
                }else{
                   $rOrder[]='rank';
                }
                ++$i;
            }

            $partition = implode(',', $col);
            
            //解析分组排名基础数据
            $res = 'select '
                   .$tb.'.'.'*'
                   .','
                   .'dense_rank() over (partition by '.$partition.' order by '.$orderby.' ) as rank '
                   .' from ('
                   .$baseSQL.') as '.$tb;
            
            //获取中文对照关系
            $Inner = $this->getInnerData($this->yAxis);
            $countColumn = $this->parseCountResultColumnExp($this->vAxis);
            
            //获取排名数据----设置的前几位
            $sql = 'select rank'
                  .','
                  .$Inner['select']
                  .','
                  .$countColumn
                  .' from ('.$res.') as tttt '
                  .$Inner['inner']
                  .' where rank <= '.$topNum
                  .' order by '.  implode(',', $rOrder);
        }
        $result = M()->query($sql);
        //echo $sql;exit();
        return $result;
    }
    
    /**
     * @获取无x维度时分析结果
     * @return type
     */
    private function getNoxAxisData(){
        //获取基础的数据源SQL
        $baseSQL = $this->getDataSelectOrigin();
        
        $Inner = $this->getInnerData($this->yAxis);
        
        $countColumn = $this->parseCountResultColumnExp($this->vAxis);
        
        $where = in_array("DUTYSS", $this->allAxis) ? " where DUTYSS <> '' and DUTYSS <> '-' " : "";

        $sql = 'select '
               .$Inner['select']
               .','
               .$countColumn
               .' from ('.$baseSQL.') as tttt '
               .$Inner['inner']
               .$where
               .' order by '.$Inner['order'];
        $result = M()->query($sql);
       // dump(in_array("DUTYSS", $this->allAxis));exit();
//        echo $sql; exit();
        return $result;
    }
    
    
    /**
     * @获取无y维度时分析结果
     * @return type
     */
    private function getNoyAxisData(){
        //获取基础的数据源SQL
        $baseSQL = $this->getDataSelectOrigin();
        
        $Inner = $this->getInnerData($this->xAxis);
        //dump($this->xAxis);
        $countColumn = $this->parseCountResultColumnExp($this->vAxis);
        $where = in_array("DUTYSS", $this->allAxis) ? " where DUTYSS <> '' and DUTYSS <> '-' " : "";
        $sql = 'select '
               .$Inner['select']
               .','
               .$countColumn
               .' from ('.$baseSQL.') as tttt '
               .$Inner['inner']
               .$where
               .' order by '.$Inner['order'];

        $result = M()->query($sql);
	
     //  dump(in_array("DUTYSS", $this->allAxis)?" where DUTYSS <> ''":"");exit();
        //echo $sql;exit();
        
        return $result;
    }
    
    /**
     * @获取xyv维度时分析结果
     * @return type
     */
    private function getXYVAxisData(){
        //获取基础的数据源SQL
        $baseSQL = $this->getDataSelectOrigin();

        $Inner = $this->getInnerData($this->allAxis);
        $countColumn = $this->parseCountResultColumnExp($this->vAxis);
        $where = in_array("DUTYSS", $this->allAxis) ? " where DUTYSS <> '' and DUTYSS <> '-' " : "";
        $sql = 'select '
               .$Inner['select']
               .','
               .$countColumn
               .' from ('.$baseSQL.') as tttt '
               .$Inner['inner']
               .$where
               .' order by '.$Inner['order'];
        //echo $sql;exit();

        $result = M()->query($sql);

        return $result;
    }
    
    /*
     * *************************************************************************
     * 以上为解析行结果数组----------------行数据--------排名
     * *************************************************************************
     */
    /**
     * @方法：将日期型结果，转换成用于挖掘的表达式
     * @param type $type
     * @param type $value
     * @return type
     */
    private function getDateDigFormat($key,$value){
        switch ($this->AxisConfig[$key]['Expression']){
            case 'Y':
                $result = mb_substr($value,0,4);
                break;
            case 'YM':
                $left = mb_substr($value,0,4);
                $right = mb_substr($value,7,2);
                $result = $left.'-'.$right;
                break;
            case 'YQ':
                $left = mb_substr($value,0,4);
                $right = mb_substr($value,10,1);
                $result = $left.'-'.$right;
                break;
            case 'YW'://周数
                $left = mb_substr($value,0,10);
                $right = mb_substr($value,13,10);
                $result = $left.'|'.$right;
                break;
            case 'YB':
                $left = mb_substr($value,0,4);
                $right = mb_substr($value,7,3);
                if($right=='上'){
                    $result = $left.'-'.'1';
                }else{
                    $result = $left.'-'.'2';
                }                
                break;
            case 'HH':
                $result = $value;
                break;
            case 'D':
            default :
                $result = $value;
                break;
        }
        return $result;
    }
    
    
    /**
     * @解析每行数据，包括增加行及列ID标识，统计字段的精度
     * @param type $data
     * @return type
     */
    private function formatMulitOutData($data){
        $result=array();
        $Left=array();
        $i=0;

        foreach ($data as $rows){
            foreach($this->vAxis as $val){
                //解析y轴数据--即行的ID
                $rid = array();        

                foreach($this->yAxis as $key){
                    //解析y轴数据--即行的ID
                    switch ($this->AxisConfig[$key]['FieldType']){
                        case 1:
                            $field = 'T_'.$key;
                            $result[$i][$key] = $rows[$field];//显示名称
                            $rid[] = $rows[$key];//id值
                            break;
                        case 2:
                            $result[$i][$key] = $rows[$key];//显示名称
                            $rid[] = $this->getDateDigFormat($key,$rows[$key]);//id值
                            break;
                        case 3:
                            $result[$i][$key] = $this->regionColumn[$key][$rows[$key]]['t'];//显示名称
                            $rid[] = $this->regionColumn[$key][$field]['n'];//20170504
                            //$rid[] = $this->regionColumn[$key][$rows[$key]]['i'].'-'.$this->regionColumn[$key][$rows[$key]]['a'];//id值
                            break;
                        case 4:
                            $result[$i][$key] = $rows[$key];//显示名称
                            $rid[] = $rows[$key];//id值
                            break;
                    }
                }
                //行ID
                $rNO = implode('@', $rid);
                $result[$i]['rid'] = $rNO;
                
                if(!in_array($rNO, $Left)){
                    $Left[] = $rNO;
                }                
                
                //解析表头字段
                $cid = array();
                $ctitle = array();

                foreach($this->xAxis as $key){
                    switch ($this->AxisConfig[$key]['FieldType']){
                        case 1:
                            $field = 'T_'.$key;
                            $ctitle[] = $rows[$field];//显示名称
                            $cid[] = $rows[$key];//id值
                            break;
                        case 2:
                            $ctitle[] = $rows[$key];//显示名称
                            $cid[] = $this->getDateDigFormat($key,$rows[$key]);//id值
                            break;
                        case 3:
                            $ctitle[] = $this->regionColumn[$key][$rows[$key]]['t'];//显示名称
                            $cid[] = $this->regionColumn[$key][$field]['n'];//20170504
                            //$cid[] = $this->regionColumn[$key][$rows[$key]]['i'].'-'.$this->regionColumn[$key][$rows[$key]]['a'];//id值
                            break;
                        case 4:
                            $ctitle[] = $rows[$key];//显示名称
                            $cid[] = $rows[$key];//id值
                            break;
                    }                
                }
                
                //追加统计方式的ID值
                $rKey = $this->setColumnResultFormat($val['k'],$val['c']);
                $cid[] = $rKey;
                //追加统计方式的名称值
                $ctitle[] = $this->parseCountName($val);
                //每个表头字段的ID及TITLE
                $cKey = implode('@', $cid);
                $cName = implode('@', $ctitle);
                
                $this->Header[$cKey] = $cName;
            
                $result[$i]['cid'] = $cKey;
                $nums = round($rows[$rKey],$this->AxisConfig[$val['k']]['Accuracy']);
                $result[$i][$cKey] = $nums;
                
                
                //需要行合计--右侧
                if($this->count['isXSum']>0){
                    $xSum[$rNO][$rKey][] = $nums;
                }
                //需要纵合计--底部
                if($this->count['isYSum']>0){
                    $ySum[$cKey][$rKey][] = $nums;
                }
            
                ++$i;
            }
        }
        
        //将左侧数据的键与值转换
        $this->Left = array_flip($Left);
        
        //需要行合计--右侧
        if($this->count['isXSum']>0){
            foreach($xSum as $key=>$val){
                foreach($val as $k=>$v){
                    $this->Right[$key][$k] = $this->setTotalNums($k,$v);
                }
            }
        }
        //需要纵合计--底部
        if($this->count['isYSum']>0){
            foreach($ySum as $key=>$val){
                foreach($val as $k=>$v){
                    $nums = $this->setTotalNums($k,$v);
                    $this->Footer[$key] = $nums;
                    $total[$k][] = $nums;
                }
                
            }
            
            if($this->count['isXSum']>0){
                if($this->vNums>1){
                    foreach($this->vAxis as $val){
                        $tKey = $this->setColumnResultFormat($val['k'], $val['c']);
                        $this->Footer[$tKey] = $this->setTotalNums($tKey,$total[$tKey]);
                    }
                }else{
                    $this->Footer['gTotal'] = $this->setTotalNums($k,$total);
                }                
            }
        }
        
        return $result;
    }
    
    /**
     * @单个V值时数据格式化
     * @param type $data
     */
    private function formatSingleOutData($data){
        $result=array();
        $Left=array();

        foreach ($data as $i=>$rows){
            //解析y轴数据--即行的ID
            $rid = array();        
            
            foreach($this->yAxis as $key){
                //解析y轴数据--即行的ID
                switch ($this->AxisConfig[$key]['FieldType']){
                    case 1:
                        $field = 'T_'.$key;
                        $result[$i][$key] = $rows[$field];//显示名称
                        $rid[] = $rows[$key];//id值
                        break;
                    case 2:
                        $result[$i][$key] = $rows[$key];//显示名称
                        $rid[] = $this->getDateDigFormat($key,$rows[$key]);//id值
                        break;
                    case 3:
                        $result[$i][$key] = $this->regionColumn[$key][$rows[$key]]['t'];//显示名称
                        //$rid[] = $this->regionColumn[$key][$rows[$key]]['i'].'-'.$this->regionColumn[$key][$rows[$key]]['a'];//id值
                        $rid[] = $this->regionColumn[$key][$rows[$key]]['n'];//id值
                        break;
                    case 4:
                        $result[$i][$key] = $rows[$key];//显示名称
                        $rid[] = $rows[$key];//id值
                        break;
                }
            }
            //行ID
            $rNO = implode('@', $rid);
            $result[$i]['rid'] = $rNO;

            if(!in_array($rNO, $Left)){
                $Left[] = $rNO;
            }
            
            //解析表头字段
            $cid = array();
            $ctitle = array();
            
            foreach($this->xAxis as $key){
                switch ($this->AxisConfig[$key]['FieldType']){
                    case 1:
                        $field = 'T_'.$key;
                        $ctitle[] = $rows[$field];//显示名称
                        $cid[] = $rows[$key];//id值
                        break;
                    case 2:
                        $ctitle[] = $rows[$key];//显示名称
                        $cid[] = $this->getDateDigFormat($key,$rows[$key]);//id值
                        break;
                    case 3:
                        $ctitle[] = $this->regionColumn[$key][$rows[$key]]['t'];//显示名称
                        $cid[] = $this->regionColumn[$key][$rows[$key]]['i'].'-'.$this->regionColumn[$key][$rows[$key]]['a'];//id值
                        break;
                    case 4:
                        $ctitle[] = $rows[$key];//显示名称
                        $cid[] = $rows[$key];//id值
                        break;
                }                
            }
            
            //每个表头字段的ID及TITLE
            $CID = implode('@', $cid);
            
            $result[$i]['cid'] = $CID;
            $this->Header[$CID] = implode('@', $ctitle);
            
            //解析每行统计数据
            $k = $this->setColumnResultFormat($this->vAxis[0]['k'],$this->vAxis[0]['c']);
            $nums = round($rows[$k],$this->AxisConfig[$this->vAxis[0]['k']]['Accuracy']);
            
            $result[$i][$CID] = $nums;
            
            //需要行合计--右侧
            if($this->count['isXSum']>0){
                $xSum[$rNO][] = $nums;
            }
            //需要纵合计--底部
            if($this->count['isYSum']>0){
                $ySum[$CID][] = $nums;
            }
        }
        
         //20170504
        sort($Left);
        //将左侧数据的键与值转换
        $this->Left = $Left;//$this->getSortArrayValue($Left);

        //需要行合计--右侧
        if($this->count['isXSum']>0){
            $k = $this->setColumnResultFormat($this->vAxis[0]['k'],$this->vAxis[0]['c']);
            foreach($xSum as $key=>$val){
                $this->Right[$key] = $this->setTotalNums($k,$val);
            }
        }
        //需要纵合计--底部
        if($this->count['isYSum']>0){
            $k = $this->setColumnResultFormat($this->vAxis[0]['k'],$this->vAxis[0]['c']);
            foreach($ySum as $key=>$val){
                $nums = $this->setTotalNums($k,$val);
                $this->Footer[$key] = $nums;
                $total[] = $nums;
            }
            
            if($this->count['isXSum']>0){
                $this->Footer['gTotal'] = $this->setTotalNums($k,$total);
            }
        }
        
        return $result;
    }

    /**
     * @对数组进行排序处理
     * @param type $arr
     * @return type
     */
    private function getSortArrayValue($arr){
        /*
        $array=array();
        sort($arr);
        $max = end($arr);
        for ($i=1;$i<=$max;$i++){
            $array[] = $i;
        }
         
         */
        //将左侧数据的键与值转换
        return array_flip($arr);
    }

    /**
     * @方法：格式化获取结果的输出格式
     * @param type $data
     * @return type
     */
    private function formatSelectOutData($data){
        if($this->vNums>1){//多个V值
            $result = $this->formatMulitOutData($data);
        }else{//单V值
            $result = $this->formatSingleOutData($data);
        }
        
        return $result;
    }
    
    
    /**
     * @根据合计值，格式化输入底部统计数据
     * @return type
     */
    private function getFooterOutFormat(){
        //底部合计
        if($this->count['isYSum']>0){
            $footer[0][] = array('t'=>$this->AxisConfig['gTotal']['FieldName'],'f'=>$this->yAxis[0],'c'=>$this->yNums);
            
            foreach($this->Footer as $key=>$val){
                $footer[0][] = array('t'=>$val,'f'=>$this->HeaderConfig[$key]);
            }
            
            if($this->count['isXSum']>0){
                if($this->vNums>1){
                    //var_dump($this->Footer);
                    foreach($this->vAxis as $val){
                        $field = $this->setColumnResultFormat($val['k'], $val['c']);
                        $footer[0][] = array('t'=>$this->Footer[$field],'f'=>$field);
                    }                    
                }else{
                    $footer[0][] = array('t'=>$this->Footer['gTotal'],'f'=>'gTotal');
                }                
            }
        }else{
            $footer = array();
        }
        
        return $footer;
    }

    /**
     * @方法：解析表头输bm 
     * @param type $rows
     * @return type
     */
    private function setDataHeadOut($rows){
        //解析X维度：即表头项
        foreach($this->xAxis as $key){
            switch ($this->AxisConfig[$key]['FieldType']){
                case 1:
                    $field = 'T_'.$key;
                    $title[] = $rows[$field];//显示名称
                    $cid[] = $rows[$key];//id值
                    break;
                case 2:
                    $title[] = $rows[$key];//显示名称
                    $cid[] = $rows[$key];//id值
                    break;
                case 3:
                    $title[] = $this->regionColumn[$key][$rows[$key]]['t'];//显示名称
                    $cid[] = $this->regionColumn[$key][$rows[$key]]['i'].'-'.$this->regionColumn[$key][$rows[$key]]['a'];//id值
                    break;
                case 4:
                    $title[] = $rows[$key];//显示名称
                    $cid[] = $rows[$key];//id值
                    break;
            }
        }
        //表头ID值--解析ID值的唯一字符串
        $ID = implode('@', $cid);
        $result=array('i'=>$ID,'t'=>$title);
        
        return $result;
    }
    

    /**
     * @方法：将数据传入，经过解析后，最终返回行数组
     * @生成表头数组$this->Header【但不含左边固定列】
     * @生成表头对照$this->HeaderConfig格式是：id=>field格式
     * @生成右侧合计$this->Right
     * @param type $data
     * @return type
     */
    private function setNoyAxisLineToOut($data){
        $i=0;//表头值项的代号
        //行合计
        $isXSum = $this->count['isXSum'];
        
        foreach ($data as $rows){
            //解析表头：id,title,
            $head = $this->setDataHeadOut($rows);

            //拼凑id与field对照关系
            $cid = $head['i'];//表头ID
            $field = 'B'.$i;
            $this->HeaderConfig[$cid] = $field;

            for($j=0;$j<$this->xNums;$j++){
                if($j == $this->xNums - 1){//最后一层级时---当只有一层时优先处理
                    $this->Header[$j][] = array('i'=>$cid,'f'=>$field,'t'=>$head['t'][$j]);//获得表头对应层级的显示名称
                }else{//非最后一层级
                    $title = array();
                    for($k=0;$k<=$j;$k++){
                        $title[] = $head['t'][$k];
                    }
                    $this->Header[$j][] = implode('@',$title);//获得表头对应层级的显示名称
                }                
            }
            
            //解析行数据--形成坐标级数据$rows[行][列]
            foreach ($this->vAxis as $val){
                $key = $this->setColumnResultFormat($val['k'], $val['c']);
                $num = round($rows[$key],$this->AxisConfig[$val['k']]['Accuracy']);
                $lines[$key][$cid] = $num;//行+列坐标值
                
                if($isXSum > 0){//需要右侧合计
                    $this->Right[$key][] = $num;
                }
            } 
            ++$i;           
        }
        
        
        return $lines;
    }

    
    /**
     * @方法：将数据传入，经过解析后，最终返回行数组
     * @生成表头数组$this->Header【但不含左边固定列】
     * @生成表头对照$this->HeaderConfig格式是：id=>field格式
     * @生成右侧合计$this->Right
     * @生成底部合计$this->Footer
     * @param type $data
     * @return type
     */
    private function setXYVAxisLineToOut($data){
        $result = array();
        
        $SortResult = $this->getSortResult();
        //主体数据行
        foreach ($data as $rows){
            $line = $SortResult[$rows['rid']];
            $result[$line]['id']=$rows['rid'];
            
            foreach($this->yAxis as $key){
                $result[$line][$key]=$rows[$key];
            }
            
            $field = $this->HeaderConfig[$rows['cid']];
            $result[$line][$field]=$rows[$rows['cid']]; 
            
            //需要横向合计
            if($this->count['isXSum']>0){
                if($this->vNums>1){
                    foreach($this->Right[$rows['rid']] as $key=>$val){
                        $result[$line][$key]=$val;
                        $this->Total[$key]=$val;
                    }
                }else{
                    $result[$line]['gTotal']=$this->Right[$rows['rid']];
                    $this->Total[$rows['rid']]=$this->Right[$rows['rid']];
                }                
            }
        }
        
        return $result;
    }
    
    /**
     * @将数据进行排序处理
     * @param type $arr
     */
    private function getSortResult(){
        $array = array();
        //若存在多个值
        if(strpos($this->Left,'@')){
            $arr = explode('@', $string);
        }else{
            $i=1;
            foreach($this->Left as $val){
                $array[$val]=$i;
                ++$i;
            }
        }
        return $array;
    }

        /**
     * @对多维数组进行指定字段的排序
     * @param type $multi_array
     * @param type $sort_key
     * @param type $sort
     * @return boolean
     * $arr = array( 
        ‘d' => array(‘id' => 5, ‘name' => 1, ‘age' => 7), 
        ‘b' => array(‘id' => 2,'name' => 3,'age' => 4), 
        ‘a' => array(‘id' => 8,'name' => 10,'age' => 5), 
        ‘c' => array(‘id' => 1,'name' => 2,'age' => 2) 
        ); 
     * multi_array_sort($arr,'age')
     */
    private function multi_array_sort($multi_array,$sort_key,$sort=SORT_ASC){ 
        if(is_array($multi_array)){ 
            foreach ($multi_array as $row_array){ 
                if(is_array($row_array)){ 
                    $key_array[] = $row_array[$sort_key]; 
                }else{ 
                    return false; 
                }
            } 
        }else{
            return false; 
        }
        array_multisort($key_array,$sort,$multi_array); 
        return $multi_array; 
    } 

    /**
     * @获得当无X维度时最终数据结果
     * @return type
     */
    private function getAnalyzeNoxAxisData(){
        //获取排名表头信息
        $result['head'] = $this->getNoxAxisHead();
        
        //获取表体信息
        $data = $this->getNoxAxisData();
        $result['rows'] = $this->getNoxAxisRows($data);
        
        if($this->count['isYSum']>0){
            $result['foot'] = $this->getSingleFootData();
        }
        return $result;
    }

    /**
     * @获得当无Y维度时最终数据结果
     * @return type
     */
    private function getAnalyzeNoyAxisData(){
 
        //获取总数据
        $data = $this->getNoyAxisData();
        
        //解析数据，生成：表头、右侧合计，返回表体数据
        $outData = $this->setNoyAxisLineToOut($data);
        
        //表头
        $result['head'] = $this->getNoyAxisHead();
        
        //表体
        $result['rows'] = $this->getNoyAxisRows($outData);
        
        $result['foot'] = array();
        return $result;
    }
    
    
    /**
     * @获得当同时存在XYV三个维度时最终数据结果
     * @return type
     */
    private function getAnalyzeXYVAxisData(){
      
        //获取总数据
        $data = $this->getXYVAxisData();
        
        //解析数据，生成：表头、右侧合计，返回表体数据
        $dataRows = $this->formatSelectOutData($data);

        //表头
        $result['head'] = $this->getXYVAxisHead();
        
        //表体
        $result['rows'] = $this->setXYVAxisLineToOut($dataRows);

        $result['foot'] = $this->getFooterOutFormat();
        return $result;
    }
    /*
     * *************************************************************************
     * 以上为解析行结果数组----------------行数据--------交叉分析
     * *************************************************************************
     */
    
    
    /**
     * @获取排行榜数据源：结果包括表头、行、脚数据
     * @return array
     */
    protected function getToppicMainData(){
        //获取排名表头信息
        $result['head'] = $this->getToppicHead();
        
        //获取排名数据源【部分带中文】
        
        if($this->yNums>1){//多排序维度
            $data = $this->getTopMultiAxisData();
        }else{
			
            $data = $this->getTopSingleAxisData();
        }
        $result['rows'] = $this->getToppicRows($data);
	
        
        return $result;
    }
    
    /**
     * @解析交叉分析结果数据：路由功能
     * @return type
     */
    protected function getAnalyzeMainData(){
     
        if($this->xNums>0){
            if($this->yNums>0){
                $result = $this->getAnalyzeXYVAxisData();
            }else{//无y维度时
                $result = $this->getAnalyzeNoyAxisData();
            }
        }else{//无x维度时
            $result = $this->getAnalyzeNoxAxisData();
        }
        return $result;
    }

    /*
     * *************************************************************************
     * 以上为解析数据结果----------------最终结果----包括表头、表体、表尾
     * *************************************************************************
     */
    
    
    /**
     * @删除临时表
     * @param type $tempTableArr
     * @return boolean
     */
    protected function doDropTempTable($tempTableArr){
       if(is_array($tempTableArr)&&!empty($tempTableArr)){
           $sql='';
           foreach($tempTableArr as $val){
              $sql.= "IF  EXISTS (SELECT * FROM SYSOBJECTS WHERE NAME='".$val."')";
              $sql.=" DROP TABLE ".$val.";"; 
           }
          M()->execute($sql); 
       }
       return false;
    }
}
